/**
 * 
 */
package com.jfinal.aceadmin.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * @author jason.zhu
 * @date 2014-12-2
 * @email jasonzhu@augmentum.com.cn
 */
public class ValidateUtil {

	public static final String EMAIL = "^[A-Za-z0-9]+([-_.][A-Za-z0-9]+)*@([A-Za-z0-9]+[-.])+[A-Za-z0-9]{2,5}$";
	public static final String NAME = "^[a-zA-Z0-9]{1,16}$";
	public static final String PASSWORD = "^[A-Za-z0-9]{6,16}$";
	public static final String AGE = "^(?:[1-9][0-9]?|1[01][0-9]|120)$";

	/**
	 * validate the account name
	 * 
	 * @param name
	 * @return
	 */
	public static boolean isNameValid(String name) {
		return validate(name, NAME);
	}

	/**
	 * validate the account email
	 * 
	 * @param email
	 * @return
	 */

	public static boolean isEmailValid(String email) {
		return validate(email, EMAIL);
	}

	/**
	 * validate the account password
	 * 
	 * @param password
	 * @return
	 */

	public static boolean isPasswordValid(String password) {
		return validate(password, PASSWORD);
	}

	/**
	 * validate the account age
	 * 
	 * @param age
	 * @return
	 */
	public static boolean isAgeValid(String age) {
		return validate(age, AGE);
	}

	/**
	 * used to validate the input content
	 * 
	 * @param content
	 * @param regex
	 */
	private static boolean validate(String content, String regex) {
		if (StringUtils.isBlank(content)) {
			return false;
		}
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(content);
		return m.matches();
	}

	public static void main(String[] args) {

		String gender = "jimmy.abc@163.com";
		System.out.println(isEmailValid(gender));
	}

	/**
	 * validate the account gender
	 * @param content
	 * @return
	 */
	public static boolean isGenderValid(String content) {
		if (StringUtils.isBlank(content)) {
			return false;
		}
		try {
			int gender = Integer.parseInt(content);
			if (0 == gender || 1 == gender) {
				return true;
			}
		} catch (NumberFormatException e) {
			return false;
		}
		return false;
	}
}
